﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.Globalization;
using DataAccess.DataObject;
using System.Collections.Generic;

namespace EnmineKaitlin.admin
{
    public partial class profile : Common.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["username"] != null || Session["username"].ToString() != "")
                {
                    BindDetail();
                }
            }
        }
        protected void BindDetail()
        {
            rptDetailUser.DataSource = _BUser.Get(Session["username"].ToString());
            rptDetailUser.DataBind();
        }
        protected void rptDetailUser_Update(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Update")
            {
                _OUser.UserName = ((TextBox)e.Item.FindControl("txtUserName")).Text;
                if (((TextBox)e.Item.FindControl("txtPassword")).Text != "")
                {
                    using (MD5 md5Hash = MD5.Create())
                    {
                        _OUser.Password = GetMd5Hash(md5Hash, ((TextBox)e.Item.FindControl("txtPassword")).Text);
                    }
                }
                else
                {
                    _OUser.Password = "";
                }
                _OUser.FullName = ((TextBox)e.Item.FindControl("txtFullName")).Text;
                _OUser.Email = ((TextBox)e.Item.FindControl("txtEmail")).Text;
                _OUser.PhoneNumber = ((TextBox)e.Item.FindControl("txtPhone")).Text;
                _OUser.Tel = ((TextBox)e.Item.FindControl("txtTel")).Text;
                _OUser.Gender = ((DropDownList)e.Item.FindControl("ddlGender")).SelectedValue;
                _OUser.BirthDay = DateTime.Parse(((TextBox)e.Item.FindControl("txtBirthday")).Text, culture, DateTimeStyles.NoCurrentDateDefault);
                _OUser.Address = ((TextBox)e.Item.FindControl("txtAddress")).Text;
                _OUser.Position = ((TextBox)e.Item.FindControl("txtPosition")).Text;
                _OUser.Status = ((DropDownList)e.Item.FindControl("ddlStatus")).SelectedValue;
                _OUser.IDGroup = int.Parse(((DropDownList)e.Item.FindControl("ddlUserGroup")).SelectedValue);
                if (_BUser.Update(_OUser))
                {
                    RegisterClientScriptBlock("NOTE", "<script>alert('Cập nhật thành công !');</script>");
                    BindDetail();
                }
            }
        }
        protected IList<OGroup> BindGroup()
        {
            return _BGroup.Get(0);
        }
    }
}
